package com.wnhoo.smartfishtank.api;

import com.google.gson.Gson;
import com.wnhoo.smartfishtank.api.data.DataRecordList;
import com.wnhoo.smartfishtank.api.data.LM_DataReceive;
import com.wnhoo.smartfishtank.api.data.LM_DataSend;
import com.wnhoo.smartfishtank.util.MyLog;

import org.apache.http.client.ClientProtocolException;

import java.io.IOException;

/**
 * Created by kevinwen on 2014/12/27.
 * Copyright (c) 2014/12/27.KevinWen. All rights reserved.
 */
public class LM_VoideAPI {

    private final static String TAG = "VoideAPI";
    private static final String tag = "VoideAPI";
    private static final Gson gson;
    static {

        gson = new Gson();
    }

    private final static String sensor_type = "1";
    /***
     *
     * 1.向服务器请求语音的状态
     * 2.打开、关闭语音
     * 3.向服务器请求灵敏度
     * 4.改变灵敏度
     * 5.删除一条或者所有语音
     * 6.发命令让设备录音，和请求录音结果
     * 7.请求语音训练是否成功的列表
     */


    /**
     * 请求语音训练是否成功的列表
     * @param device_id
     * @param token
     * @throws ClientProtocolException
     * @throws IOException
     * @throws Exception
     * @return DataRecordList or NULL
     */
    public static DataRecordList requestRecordList(String device_id,String token)throws ClientProtocolException, IOException ,Exception{

        //APP:/S06/1/2
        //Device:/S06/1/1，1，1，1，1，1，1，1，0，0
        final String sensor_id = "S06";
        String control_data = "2";
        MyLog.s(tag, "requestRecordList...");
        LM_BasicAPI.requestSend(true, device_id, sensor_id, sensor_type, control_data, token);
        MyLog.d(TAG, "S06/1/2 send end.");
        MyLog.d(TAG,"sleep.");
        Thread.sleep(800);
        MyLog.d(TAG,"sleep end.");
        MyLog.d(TAG,"requestReceive .");
        String result = LM_BasicAPI.requestReceive(true, device_id, sensor_id, sensor_type, token);
        LM_DataReceive dataReceive = gson.fromJson(result,LM_DataReceive.class);
        //MyLog.d(TAG,result.toString());
        //MyLog.s(tag,result.toString());
        if(result!=null&&dataReceive.getValue()!=null&&dataReceive.getValue().length()==19){
            String value[] = dataReceive.getValue().split(",");
            DataRecordList list = new DataRecordList();
            try {
                list.setRecord0(Integer.valueOf(value[0]));
                list.setRecord1(Integer.valueOf(value[1]));
                list.setRecord2(Integer.valueOf(value[2]));
                list.setRecord3(Integer.valueOf(value[3]));
                list.setRecord4(Integer.valueOf(value[4]));
                list.setRecord5(Integer.valueOf(value[5]));
                list.setRecord6(Integer.valueOf(value[6]));
                list.setRecord7(Integer.valueOf(value[7]));
                list.setRecord8(Integer.valueOf(value[8]));
                list.setRecord9(Integer.valueOf(value[9]));

            }catch (Exception e){
                MyLog.s(tag,e.toString());
                list = null;
            }finally {
                return list;
            }
        }
        return null;
    }

    /**
     * 请求录音结果
     * @param device_id
     * @param token
     * @throws ClientProtocolException
     * @throws IOException
     * @throws Exception
     */
    public static LM_DataReceive requestRecordResult(String device_id,String token)throws ClientProtocolException, IOException ,Exception{

        // /S05/1/第X条语音 , 第X次训练 , 训练结果
        final String sensor_id = "S05";
        String result = LM_BasicAPI.requestReceive(true, device_id, sensor_id, sensor_type, token);
        return gson.fromJson(result,LM_DataReceive.class);
    }

    /**
     * 发命令让设备录音
     * @param device_id
     * @param token
     * @param degree 第x次录音（x=1；x=2）
     * @param voide_id
     * @throws ClientProtocolException
     * @throws IOException
     * @throws Exception
     */
    public static LM_DataSend controlRecording(String device_id,String token,int degree,String voide_id)throws ClientProtocolException, IOException ,Exception{

        // /S05/1/第X条语音 , 第X次训练
        final String sensor_id = "S05";
        String control_data = voide_id + "," + degree;
        String result = LM_BasicAPI.requestSend(true, device_id, sensor_id, sensor_type, control_data, token);
        return gson.fromJson(result,LM_DataSend.class);
    }

    /**
     * 请求删除某一条录音的结果
     * @param device_id
     * @param token
     * @throws ClientProtocolException
     * @throws IOException
     * @throws Exception
     */
    public static LM_DataReceive requestDeletVoideForIdResult(String device_id,String token)throws ClientProtocolException, IOException ,Exception{

        //成功，/DeviceID/S05/1/1,SUCCESS;
        //失败，/DeviceID/S05/1/1,FAIL;
        final String sensor_id = "S07";
        String result = LM_BasicAPI.requestReceive(true, device_id, sensor_id, sensor_type, token);
        return gson.fromJson(result,LM_DataReceive.class);
    }

    /**
     * 删除某一条录音
     * @param device_id
     * @param id_voide 第x条语音
     * @param token
     * @throws ClientProtocolException
     * @throws IOException
     * @throws Exception
     */
    public static  LM_DataSend controlDeletVoideForId(String device_id,String id_voide,String token)throws ClientProtocolException, IOException ,Exception{

        // /DeviceID/S05/1/1,DEL
        final String sensor_id = "S07";
        String control_data = id_voide + ",DEL";
        String result = LM_BasicAPI.requestSend(true, device_id, sensor_id, sensor_type, control_data, token);
        return gson.fromJson(result,LM_DataSend.class);
    }

    /**
     * 请求删除某一条录音的结果
     * @param device_id
     * @param token
     * @throws ClientProtocolException
     * @throws IOException
     * @throws Exception
     */
    public static LM_DataReceive requestDeletAllVoideResult(String device_id,String token)throws ClientProtocolException, IOException ,Exception{

        //成功，/DeviceID/S05/1/0,SUCCESS;
        //失败，/DeviceID/S05/1/0,FAIL;
        final String sensor_id = "S07";
        String result = LM_BasicAPI.requestReceive(true, device_id, sensor_id, sensor_type, token);
        return gson.fromJson(result,LM_DataReceive.class);
    }

    /**
     * 删除所有的录音
     * @param device_id
     * @param token
     * @throws ClientProtocolException
     * @throws IOException
     * @throws Exception
     */
    public static LM_DataSend controlDeletAllVoide(String device_id,String token)throws ClientProtocolException, IOException ,Exception{

        // /DeviceID/S05/1/0,DEL
        final String sensor_id = "S07";
        String control_data = "0,DEL";
        String result = LM_BasicAPI.requestSend(true, device_id, sensor_id, sensor_type, control_data, token);
        return gson.fromJson(result,LM_DataSend.class);
    }

    /**
     * 更改当前设备语音识别的灵敏度
     * @param device_id
     * @param sensitive (3,4,5)
     * @param token
     * @throws ClientProtocolException
     * @throws IOException
     * @throws Exception
     */
    public static LM_DataSend controlSensitivity(String device_id,int sensitive,String token)throws ClientProtocolException, IOException ,Exception{

        //高： /DeviceID/S04/1/3
        //中： /DeviceID/S04/1/4
        //底： /DeviceID/S04/1/5
        final String sensor_id = "S04";
        String control_data = String.valueOf(sensitive);
        String result = LM_BasicAPI.requestSend(true, device_id, sensor_id, sensor_type, control_data, token);
        return gson.fromJson(result,LM_DataSend.class);
    }
    /**
     * 向服务器请求当前设备语音识别的灵敏度（2,3,4）
     */
    public static LM_DataReceive requestSensitivity(String device_id,String token)throws ClientProtocolException, IOException ,Exception{

        //高： /DeviceID/S04/1/3
        //中： /DeviceID/S04/1/4
        //底： /DeviceID/S04/1/5
        final String sensor_id = "S04";

        String result = LM_BasicAPI.requestReceive(true, device_id, sensor_id, sensor_type, token);
       return gson.fromJson(result, LM_DataReceive.class);
    }
    /**
     * 向服务器请求当前设备语音识别的状态（开启还是关闭）
     */
    public static LM_DataReceive requestStatusVoide(String device_id,String token)throws ClientProtocolException, IOException ,Exception{

        //设备：收到什么回复什么
        //开:/S03/1/1
        //关:/S03/1/0
        final String sensor_id = "S03";
        String result = LM_BasicAPI.requestReceive(true, device_id, sensor_id, sensor_type, token);
        return gson.fromJson(result,LM_DataReceive.class);

    }

    /**
     * 打开、关闭语音识别
     *
     * @param device_id
     * @param status 1或者0
     * @param token
     * @throws ClientProtocolException
     * @throws IOException
     * @throws Exception
     */
    public static LM_DataSend controlStatusVoide(String device_id,int status, String token)throws ClientProtocolException, IOException ,Exception{

        //开，APP:/S03/1/1
        //关，APP:/S03/1/0
        final String sensor_id = "S03";
        String control_data = String.valueOf(status);
        String result = LM_BasicAPI.requestSend(true, device_id, sensor_id, sensor_type, control_data, token);
        return gson.fromJson(result,LM_DataSend.class);
    }

    /**
     * Debug S0x 的内容
     * @param device_id
     * @param sensor_id
     * @param token
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     * @throws Exception
     */
    public static LM_DataReceive requestDebug(String device_id, String sensor_id,String token)throws ClientProtocolException, IOException ,Exception{

        String result = LM_BasicAPI.requestReceive(true, device_id, sensor_id, sensor_type, token);
        return gson.fromJson(result,LM_DataReceive.class);
    }

}
